package br.com.fogas.medicao.activity.helper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.fogas.database.FogasDatabase;
import br.com.fogas.database.entidade.PlanoVisitas;
import br.com.fogas.database.enums.PlanoVisitaStatusEnum;
import br.com.fogas.medicao.R;
import br.com.fogas.medicao.activity.PlanoClientesFragment;
import br.com.fogas.utils.dialog.DialogAlert;
import br.com.fogas.medicao.adapter.AdapterOnClickView;
import br.com.fogas.medicao.adapter.PlanoLocalAdapter;
import br.com.fogas.medicao.application.Constants;
import br.com.fogas.medicao.application.LoadFragment;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnItemClick;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

public class PlanoLocalHelper {
	
	private Fragment fragment;
	
	private FogasDatabase database;
	
	private List<PlanoVisitas> planos;
	
	private PlanoLocalAdapter planoAdapter;
	
	@InjectView(R.id.lvPlanoShopping) ListView lvPlanoShoppings;
	
	@InjectView(R.id.btnFinalizarPlano) Button btnFinalizarPlano;
	
	public PlanoLocalHelper(Fragment fragmentParent) {
		ButterKnife.inject(this, fragmentParent.getView());
		
		fragment = fragmentParent;
		database = new FogasDatabase(fragment.getActivity());
		
		planos = new ArrayList<PlanoVisitas>();	
		planoAdapter = new PlanoLocalAdapter(fragment.getView().getContext(), visitarClick, planos);
		
		lvPlanoShoppings.setAdapter(planoAdapter);
		
		atualizarTela();
	}
	
	public void onDestroy() {
		database.fecharConexao();
		ButterKnife.reset(this);
	}
	
	@OnItemClick(R.id.lvPlanoShopping)
	public void clickLocal(int position) {
		PlanoVisitas planoVisitas = (PlanoVisitas) lvPlanoShoppings.getItemAtPosition(position);
		
		Fragment fragmentClientes = PlanoClientesFragment.newInstance(planoVisitas);
		LoadFragment loadFragment = new LoadFragment(fragment.getActivity().getSupportFragmentManager());
		loadFragment.initializeFragment(true, fragmentClientes, Constants.FRAGMENT_PLANO_CLIENTES);
	}
	
	@OnClick(R.id.btnFinalizarPlano)
	public void finalizarPlano(Button button) {	
		if(podeFinalizarPlano()) {
			
		}
	}
	
	private final AdapterOnClickView visitarClick = new AdapterOnClickView() {
		
		@Override
		public void onClick(View view, Object object) {
			try {
				CheckBox checkBox = (CheckBox) view;
				PlanoVisitas planoLocal = (PlanoVisitas) object;
				PlanoVisitaStatusEnum status = database.getPlanoVisitas().statusVisitaLocal(planoLocal.getCliente());
				
				if(checkBox.isChecked()) {
					eventChecked(checkBox, planoLocal, status);
				} else {
					eventUnchecked(checkBox, planoLocal, status);
				}
				atualizarTela();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	};
	
	private void eventChecked(CheckBox checkBox, PlanoVisitas planoLocal, PlanoVisitaStatusEnum status) {
		try {
			database.getPlanoVisitas().deixarPendenteVisitaLocalVisitaClientes(planoLocal);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void eventUnchecked(CheckBox checkBox, PlanoVisitas planoLocal, PlanoVisitaStatusEnum status) {
		try {
			List<PlanoVisitas> visitaClientesFinalizadas = database.getPlanoVisitas()
					.listarPlanoClientesPorLocalStatus(planoLocal.getCliente(), PlanoVisitaStatusEnum.FINALIZADO);
			if(visitaClientesFinalizadas.size() == 0) {
				database.getPlanoVisitas().cancelarVisitaLocalVisitaClientes(planoLocal);
			} else {
				// se tiver 1 plano cliente finalizado não pode desmarcar
				checkBox.setChecked(true); 
				DialogAlert.newInstance()
				.setTitle(R.string.dialog_title_alert)
				.setCharMessage(fragment.getActivity().getString(R.string.plano_visita_nao_pode_cancelar))
				.showDialog(fragment.getActivity().getSupportFragmentManager());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void atualizarTela() {
		try {
			planos.clear();
			planos.addAll(database.getPlanoVisitas().listarPlanoLocais());
			planoAdapter.notifyDataSetChanged();
			lvPlanoShoppings.invalidateViews();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private boolean podeFinalizarPlano() {
		boolean habilitar = true;
		
		for (PlanoVisitas plano : getPlanos()) {
			if (plano.getStatusEnum().equals(PlanoVisitaStatusEnum.PENDENTE)) {
				habilitar = false;
				break;
			}
		}
		
		if(!habilitar) {
			DialogAlert.newInstance()
			.setTitle(R.string.dialog_title_alert)
			.setCharMessage(fragment.getActivity().getString(R.string.plano_visita_nao_finalizar_plano))
			.showDialog(fragment.getActivity().getSupportFragmentManager());
		}
		
		return habilitar;
	}
	
	public ListView getLvPlanoShoppings() {
		return lvPlanoShoppings;
	}
	
	public PlanoLocalAdapter getPlanoAdapter() {
		return planoAdapter;
	}
	
	public List<PlanoVisitas> getPlanos() {
		return planos;
	}
	
	public Fragment getFragment() {
		return fragment;
	}
	
	public FogasDatabase getDatabase() {
		return database;
	}
	
	public Button getBtnFinalizarPlano() {
		return btnFinalizarPlano;
	}
}